package cn.powertime.evaluation.app.controller;


import cn.powertime.evaluation.Constants;
import cn.powertime.evaluation.entity.CpsChapter;
import cn.powertime.evaluation.entity.CpsResource;
import cn.powertime.evaluation.entity.CpsUserLearningRecord;
import cn.powertime.evaluation.facade.CpsCourseFacade;
import cn.powertime.evaluation.service.CpsChapterService;
import cn.powertime.evaluation.service.CpsCourseService;
import cn.powertime.evaluation.service.CpsTeacherService;
import cn.powertime.evaluation.service.CpsUserLearningRecordService;
import cn.powertime.evaluation.shiro.ShiroUtils;
import cn.powertime.evaluation.shiro.jwt.TokenUtils;
import cn.powertime.evaluation.vo.*;
import cn.powertime.evaluation.web.controller.BaseController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author liqi
 * @since 2019-09-23
 */
@RestController
@RequestMapping("/app/cps-course")
@Api(value = "/app/cps-course", tags = "移动端课程接口", produces = MediaType.ALL_VALUE)
@Validated
public class AppCpsCourseController extends BaseController  {

    @Autowired
    private CpsCourseFacade cpsCourseFacade;

    @Autowired
    private CpsChapterService cpsChapterService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CpsUserLearningRecordService cpsUserLearningRecordService;

    @Autowired
    private TokenUtils tokenUtils;

    @ApiOperation(value = "课程分页列表",response = WebRespCourseListVo.class)
    @PostMapping(value = "/list")
    public Object list(@Validated @RequestBody ParamPageVo<WebReqCourseSearchVo> vo, HttpServletRequest request) {
        IPage<WebRespCourseListVo> list = cpsCourseFacade.list(vo,tokenUtils.getUid(request));
        return success(list);
    }

    @ApiOperation(value = "课程章节列表",response = WebRespChapterListVo.class)
    @PostMapping(value = "/chapter")
    public Object chapterList( ) {
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        String  WebRespCourseAllListVo = stringRedisTemplate.opsForValue().get(userInfo.getId() + "-course");
        List<WebRespChapterListVo> list=new ArrayList<>();
        List<CpsChapter> list1=new ArrayList<>();
        if ( WebRespCourseAllListVo != null) {
            Long courseId = Long.parseLong(WebRespCourseAllListVo);
            QueryWrapper<CpsChapter> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",Constants.STATUS_NORMAL);
            queryWrapper.eq("course_id",courseId);
            queryWrapper.eq("pid",0);
            list1=cpsChapterService.list(queryWrapper);
                 //list = cpsCourseFacade.chapterList(courseId,tokenUtils.getUid(request));
           // }
        }
       return success(list1);
    }

    @ApiOperation(value = "课程资源列表",response = WebRespCourseResourceListVo.class)
    @PostMapping("/courseResourceList")
    public Object courseResourceList(@Validated @RequestBody WebReqResourceVo vo) {
        List<WebRespCourseResourceListVo> list = cpsCourseFacade.courseResourceList(vo);
        return success(list);
    }

    @ApiOperation(value = "学习进度打点")
    @PostMapping("/record")
    public Object record(@Validated @RequestBody WebReqRecordVos vo) {
        System.out.println("观看时间"+vo.getSchedule());
        boolean b = cpsCourseFacade.Apprecord(vo);
        if(b){
            return success("记录成功");
        }
        return error("记录失败");
    }

    //@ApiOperation(value = "课程小节列表",response = WebChapterVo.class)
    @PostMapping(value = "/chapterListId")
    public Object chapterLista(@Validated @RequestBody ChapterVo vo) {
        List<WebChapterVo> list = new ArrayList<>();
        List<WebChapterVoS> list1=new ArrayList<>();
        Page page =new Page();
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        System.out.println(vo.getId());
        if (vo.getId().equals("0")){
            String  WebRespCourseAllListVo = stringRedisTemplate.opsForValue().get(userInfo.getId() + "-course");
            if ( WebRespCourseAllListVo != null) {
                Long courseId = Long.parseLong(WebRespCourseAllListVo);
                page=PageHelper.startPage(vo.getPage(),vo.getSize());
                AppCourseVo vo1=new AppCourseVo();
                vo1.setCourseId(courseId);
                vo1.setUserId(userInfo.getId());
                list= cpsChapterService.chapterTeherList(vo1);
            }
        }else {
            AppCourseVo vo1=new AppCourseVo();
            vo1.setCourseId(Long.parseLong(vo.getId()));
            vo1.setUserId(userInfo.getId());
            page= PageHelper.startPage(vo.getPage(),vo.getSize());
            list= cpsChapterService.chapterListType(vo1);

        }
        Double dd;
        for(int i=0;i<list.size();i++){
            WebChapterVoS webChapterVoS = new WebChapterVoS();
            Long resourcesize =list.get(i).getResourcesize();
            Double  schedule=list.get(i).getSchedule();
            if(schedule==null){
                 dd=0.00;
            }else {
                 dd = schedule/(resourcesize * 1.0);
            }
            BigDecimal bg = new BigDecimal(dd);
            Double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            if(f1>1.00){
                f1=1.00;
            }
            System.out.println(f1);
            webChapterVoS.setChapterfileid(list.get(i).getChapterfileid());
            webChapterVoS.setChapterName(list.get(i).getChapterName());
            webChapterVoS.setCoursefileid(list.get(i).getCoursefileid());
            webChapterVoS.setCourseName(list.get(i).getCourseName());
            webChapterVoS.setFileId(list.get(i).getFileId());
            webChapterVoS.setId(list.get(i).getId());
            webChapterVoS.setPid(list.get(i).getPid());
            webChapterVoS.setSchedule(list.get(i).getSchedule());
            webChapterVoS.setResourcesize(list.get(i).getResourcesize());
            webChapterVoS.setCourseId(list.get(i).getCourseId());
            webChapterVoS.setPercentage(f1);
            webChapterVoS.setDes(list.get(i).getDes());
            webChapterVoS.setIntroduce(list.get(i).getIntroduce());
            webChapterVoS.setCredit(list.get(i).getCredit());
            webChapterVoS.setPosition(list.get(i).getPosition());
            webChapterVoS.setType(list.get(i).getType());
            list1.add(webChapterVoS);
        }
        PageInfo pageInfo = new PageInfo(list1);
        return success1(list1,pageInfo.getPages(),pageInfo.getPageSize(), page.getTotal());
    }

//    @ApiOperation(value = "课程学习记录列表",response = WebRespCourseRecordListVo.class)
//    @PostMapping(value = "/recordList")
//    public Object recordList(@Validated @RequestBody PageVo vo) {
//        IPage<WebRespCourseRecordListVo> list = cpsCourseFacade.recordList(vo);
//        return success(list);
//    }
    @ApiOperation(value = "课程推荐列表",response = WebRespChapterListVo.class)
    @PostMapping(value = "/commend")
    public  Object commend(@Validated @RequestBody ChapterVo vo){
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        String  WebRespCourseAllListVo = stringRedisTemplate.opsForValue().get(userInfo.getId() + "-course");
        List<CpsChapter> cpsChapters=new ArrayList<>();
        Page page = new Page();
        if ( WebRespCourseAllListVo != null) {
                Long courseId = Long.parseLong(WebRespCourseAllListVo);
                page=PageHelper.startPage(vo.getPage(),vo.getSize());
                QueryWrapper<CpsChapter> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("status", Constants.STATUS_NORMAL);
                queryWrapper.eq("course_id", courseId);
                queryWrapper.ne("pid", 0);
                queryWrapper.orderByAsc("create_time");
                cpsChapters = cpsChapterService.list(queryWrapper);
        }
        PageInfo pageInfo = new PageInfo(cpsChapters);
        return success1(cpsChapters,pageInfo.getPages(),pageInfo.getPageSize(),page.getTotal());
    }


}
